package com.key.service.impl;

import com.key.entity.SysMenu;
import com.key.entity.SysRole;
import com.key.mapper.SysRoleMapper;
import com.key.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：朕
 * @Package：com.key.service.impl
 * @Project：ams_system
 * @name：SysRoleServiceImpl
 * @Date：2023/2/21 20:36
 * @Filename：SysRoleServiceImpl
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 显示用户
     * @return
     */
    @Override
    public List<SysRole> findRole() {
        List<SysRole> sysRoles = sysRoleMapper.findRole();
        return sysRoles;
    }

    /**
     * 增加角色
     * @return
     */
    @Override
    public boolean addRole(SysRole sysRole) {
        List<SysRole> title = sysRoleMapper.findRoleByRoleTitle(sysRole.getRoleTitle());
        if(title.size()==0){
            int a = sysRoleMapper.addRole(sysRole);
            return true;
        }
        return false;
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @Override
    public boolean deleteRole(Integer roleId) {
        int s = sysRoleMapper.deleteRole(roleId);
        if(s>0){
            return true;
        }
        return false;
    }

    /**
     * 根据角色Id查询角色信息
     * @param roleTitle
     * @return
     */
    @Override
    public List<SysRole> findRoleByRoleTitle(String roleTitle) {
        List<SysRole> sysRoles = sysRoleMapper.findRoleByRoleTitle(roleTitle);
        return sysRoles;
    }

    /**
     * 修改角色信息
     * @param sysRole
     * @return
     */
    @Override
    public boolean updateRole(SysRole sysRole) {
        Integer s = sysRoleMapper.updateRole(sysRole);
        if(s>0){
            return  true;
        }else{
            return false;
        }
    }

    /**
     * 显示权限
     * @return
     */
    @Override
    public List<SysMenu> findMenu() {
        List<SysMenu> sysMenus = sysRoleMapper.findMenu();
        return sysMenus;
    }

    /**
     * 根据角色Id查询权限
     * @param roleId
     * @return
     */
    @Override
    public List<SysMenu> findMenuByRoleId(Integer roleId) {
        List<SysMenu> sysMenus = sysRoleMapper.findMenuByRoleId(roleId);
        return sysMenus;
    }

    /**
     * 先删后增角色权限
     * @param roleId
     * @param menuId
     * @return
     */
    @Override
    public boolean deleteAddRoleMenu(Integer roleId, String menuId) {
        int s = sysRoleMapper.deleteMenuByRoleId(roleId);
        if(menuId==null || !" ".equals(menuId)){
            String[] ids = menuId.split(",");
            for(String id:ids){
                Map<String,Integer> map = new HashMap<>();
                map.put("userId",roleId);
                map.put("roleId",Integer.parseInt(id));
                sysRoleMapper.addRoleMenu(map);
                return true;
            }
        }
        return false;
    }

}
